<template>
  <div class="m-user">
    <template v-if="user">
      欢迎您，<span class="username">{{ user }}</span>
      [
      <span
        style="cursor: pointer;color: #13D1BE;"
        @click="logout">退出</span>
      ]
    </template>
    <template v-else>
      <nuxt-link
        to="/login"
        class="login">立即登陆
      </nuxt-link>
      <nuxt-link
        to="/register"
        class="register">注册
      </nuxt-link>
    </template>
  </div>
</template>

<script>
export default {
  name: 'User',
  data() {
    return {
      user: ''
    }
  },
  async mounted() {
    const { status, data } = await this.$axios.get('/user/getUserInfo')
    if (status == 200 && data.code == 0) {
      this.user = data.username
    }
  },
  methods: {
    async logout() {
      const { status, data } = await this.$axios('/user/signout')
      if (status == 200 && data.code == 0) {
        this.user = ''
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
